package com.centling.smartSealForPhone.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.activity.AddUserActivity;
import com.centling.smartSealForPhone.activity.UserDetailActivity;
import com.centling.smartSealForPhone.adapter.UserListAdapter;
import com.centling.smartSealForPhone.bean.ManagedUser;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.utils.OkHttpUtil;
import com.centling.smartSealForPhone.utils.ShowToast;
import com.centling.smartSealForPhone.widget.AutoRecyclerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;


public class UserManageFragment extends TitleBarFragment {
    private int pageNo = 0;

    private PtrClassicFrameLayout ptrClassicFrameLayout;
    private AutoRecyclerView recyclerView;
    private List<ManagedUser> list = new ArrayList<>();

    public static UserManageFragment getInstance() {
        UserManageFragment fragment = new UserManageFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int setLayoutResource() {
        return R.layout.fragment_user_manage;
    }

    @Override
    public void findViewInThisFunction(View rootView) {
        setTitleBarText("用户管理");
        //setTitleBarRight(R.drawable.ic_add);
        setTitleBarRight(R.drawable.iv_add_plus);
        setTitleBarRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, AddUserActivity.class);
                startActivity(intent);
            }
        });
        initView(rootView);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    public void initView(View view) {
        ptrClassicFrameLayout = (PtrClassicFrameLayout) view.findViewById(R.id.user_manage_layout);
        recyclerView = (AutoRecyclerView) view.findViewById(R.id.user_manage_list);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 1));
        UserListAdapter adapter = new UserListAdapter(UserManageFragment.this.getActivity(), list);
        recyclerView.setAdapter(adapter);
        ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                sendRequest(true);
            }
        });

        adapter.setOnItemClickListener(new UserListAdapter.onItemClickListener() {
            @Override
            public void onClick(int position) {
                String json = new Gson().toJson(list.get(position));
                Intent intent = new Intent(UserManageFragment.this.getActivity(), UserDetailActivity.class);
                intent.putExtra("user_info", json);
                startActivity(intent);
            }
        });


    }

    private void sendRequest(final boolean isRefresh) {
        if (isRefresh) {
            pageNo = 0;
        }
        getEnqueue(UrlConstants.GET_USERS + pageNo, new OkHttpUtil.NetCallBack() {
            @Override
            public void onSucceed(String json) {
                try {
                    if (isRefresh) {
                        list.clear();
                        recyclerView.setLoadDataListener(new AutoRecyclerView.LoadDataListener() {
                            @Override
                            public void onLoadMore() {
                                sendRequest(false);
                            }
                        });
                    }
                    JSONObject data = new JSONObject(json);
                    List<ManagedUser> tmpList;
                    tmpList = new Gson().fromJson(data.getJSONArray("rows").toString(), new TypeToken<List<ManagedUser>>() {
                    }.getType());
                    list.addAll(tmpList);
                    pageNo++;
                    recyclerView.getAdapter().notifyDataSetChanged();
                    ptrClassicFrameLayout.refreshComplete();

                    if (list.size() < 20) {
                        recyclerView.loadMoreComplete(true);
                    } else {
                        recyclerView.loadMoreComplete(false);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailed(String reason) {
                recyclerView.loadMoreComplete(false);
                ptrClassicFrameLayout.refreshComplete();
                ShowToast.shortTime(reason);
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            sendRequest(true);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        recyclerView.post(new Runnable() {
            @Override
            public void run() {
                ptrClassicFrameLayout.autoRefresh();
            }
        });
    }
}
